﻿using System;
using System.Collections.Generic;
using System.Text;
using Galaxy.ModuleInterface;
using Galaxy.ModuleInterface.Messages;
using Galaxy.ModuleInterface.Events;
using Galaxy.ModuleInterface.Messages.Source;
using Galaxy.ModuleInterface.Messages.Destination;

namespace Galaxy.TestModule
{
    public class CMessageSender : CGalaxyModule, IGalaxyModule
    {
        public CMessageSender() : base("Передатчик сообщений") { }

        public CMessageSender(string Name) : base(Name) { }

        public override void Start()
        {
#if DEBUG
            Console.WriteLine("Передатчик запущен");
#endif
        }

        public override void Stop()
        {
#if DEBUG
            Console.WriteLine("Передатчик остановлен");
#endif
        }

        public override void Initialize()
        {
#if DEBUG
            Console.WriteLine("Передатчик проинициализирован");
#endif
        }

        public override void ProcessMessage(IGalaxyModuleMessage Message)
        {
            InvokeNewMessage(Message);
        }

        public override void ReceiveMessage(IGalaxyModule Module, NewMessageEventArgs Arg)
        {
            if( !Arg.Message.Source.Equals(this) )
                base.ReceiveMessage(Module, Arg);
        }

        public void SendMessage(string Message, params IGalaxyModule[] Distination)
        {
            ProcessMessage(new CTextMessage(Message, this, Distination));
        }

        public void SendInvokeMessage(params IGalaxyModule[] Distination)
        {
            ProcessMessage(new CInvokeMessage(this, Distination));
        }

        public void SendMessage(IGalaxyModuleMessage Message,
            params IGalaxyModule[] Distination)
        {
            Message.Source = new CModuleSource(this);
            Message.Destination = new CDestination(Distination);
            InvokeNewMessage(Message);
        }

    }
}
